package com.ncloud.chnl.mapper;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.chnl.entity.CustAreaNode;  
@Mapper
public interface CustAreaNodeMapper{  
  
    @Select(value = {"select * from bs_cust_area_node_rel where area_id = #{areaId} and node_id = #{nodeId} and state ='1'" })
	@Results({ @Result(column = "rel_inst_id", property = "relInstId"),
			@Result(column = "area_id", property = "areaId"), @Result(column = "node_id", property = "nodeId"),
			@Result(column = "state", property = "state"), @Result(column = "create_time", property = "createTime"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark")})
    public CustAreaNode getCustAreaNode(@Param("areaId")Long areaId,@Param("nodeId")Long nodeId);
  
    @Select(value = {"select a.*,b.node_name from bs_cust_area_node_rel a,bs_node b where area_id = #{areaId} and state = '1'"
    		+ " and a.node_id = b.node_id and now() between b.begin_date and b.end_date and b.eff_flag ='1'" })  
	@Results({ @Result(javaType=String.class, column = "rel_inst_id", property = "relInstId"),@Result(column = "node_name", property = "nodeName"),
			@Result(javaType=String.class, column = "area_id", property = "areaId"), 
			@Result(javaType=String.class, column = "node_id", property = "nodeId"),
			@Result(column = "state", property = "state"), @Result(column = "create_time", property = "createTime"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark")}) 
    public List<Map<String,Object>> getAllCustAreaNodes(Long areaId);


    @Insert(value = ("insert into bs_cust_area_node_rel(rel_inst_id,area_id,node_id,state,create_time,create_user,update_time,update_user,remark)"
    		+ " values (#{relInstId},#{areaId},#{nodeId},#{state},#{createTime},#{createUser},#{updateTime},#{updateUser},#{remark})"))
    public int insertCustAreaNode(CustAreaNode custAreaNode);
    
    @Update(value = ("update bs_cust_area_node_rel set state = #{state},update_time = now(),"
    		+ " update_user = #{updateUser},remark = #{remark} where rel_inst_id = #{relInstId}"))
    public int updateCustAreaNode(CustAreaNode custAreaNode);

    @Update("update bs_cust_area_node_rel set state = '0',update_time = now(),update_user = #{1} where area_id = #{0}")
	public void deleteCustAreaNodes(Long areaId, Long userId);

    @Select("select b.node_id nodeId from bs_cust_area_user_rel a,bs_cust_area_node_rel b "
    		+ "where a.user_id = #{userId} and a.state = '1' and a.area_id = b.area_id and b.state = '1'")
	public List<Map<String, Object>> getNodeListByUserId(Long userId);
    
} 